package com.mpdbailey.cleverdicandroid;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.pigdogbay.androidutils.mvp.AdModel;
import com.pigdogbay.androidutils.mvp.BackgroundColorModel;
import com.pigdogbay.androidutils.mvp.IAdModel;
import com.pigdogbay.androidutils.mvp.IBackgroundColorModel;
import com.pigdogbay.androidutils.utils.PreferencesHelper;

import android.app.Activity;

public class AppData implements IApplicationModel{
	
	private Activity _Activity;	
	private PreferencesHelper _PreferencesHelper; 
	public PreferencesHelper getPreferencesHelper()
	{
		if (_PreferencesHelper==null)
		{
			_PreferencesHelper = new PreferencesHelper(_Activity);
		}
		return _PreferencesHelper;
	}

	public AppData(Activity activity)
	{
		_Activity = activity;
	}
	public IAdModel createAdModel()
	{
		return new AdModel(getPreferencesHelper());
	}
	public IBackgroundColorModel createBackgroundColorModel()
	{
		return new BackgroundColorModel(getPreferencesHelper());
	}
	public boolean showSubAnagrams()
	{
		return getPreferencesHelper().getBoolean(R.string.settings_show_subanagrams_pref_key, true);
	}
	public int getResultsLimit()
	{
		return getPreferencesHelper().getInt(R.string.settings_result_limit_pref_key, 50);
	}
	@Override
	public boolean getUnlockLargeDictionary()
	{
		return getPreferencesHelper().getBoolean(R.string.code_pref_unlock_large_dictionary_key, false);
	}
	@Override
	public void setUnlockLargeDictionary(boolean unlock)
	{
		getPreferencesHelper().setBoolean(R.string.code_pref_unlock_large_dictionary_key,unlock);
	}
	@Override
	public boolean getUseLargeDictionary()
	{
		return getPreferencesHelper().getBoolean(R.string.code_pref_use_large_dictionary_key, false);
	}
	@Override
	public void setUseLargeDictionary(boolean use) {
		getPreferencesHelper().setBoolean(R.string.code_pref_use_large_dictionary_key,use);
	}
	@Override
	public boolean isFirstTime() {
		return getPreferencesHelper().getBoolean(R.string.code_pref_is_first_time_key, true);
	}

	@Override
	public void setFirstTime(boolean firstTime) {
		getPreferencesHelper().setBoolean(R.string.code_pref_is_first_time_key,firstTime);
	}

	@Override
	public List<String> getDictionary() {
		CleverDicApplication app = (CleverDicApplication) _Activity.getApplication();
		return app.getDicitionary();
	}
	@Override
	public void setDictionary(List<String> dictionary) {
		CleverDicApplication app = (CleverDicApplication) _Activity.getApplication();
		app.setDictionary(dictionary);
	}
	@Override
	public List<String> loadStandardDictionary() throws IOException {
		return loadDictionary(R.raw.standard);
	}

	@Override
	public List<String> loadProDictionary() throws IOException {
		return loadDictionary(R.raw.pro);
	}
	private List<String> loadDictionary(int id) throws IOException
	{
		InputStream inputStream = null;
		try
		{
			inputStream = _Activity.getResources().openRawResource(id);
			return DictionaryReader.LoadDictionary(inputStream);
		} 
		finally
		{
			if (inputStream != null)
			{
				inputStream.close();
			}
		}
		
	}

	@Override
	public void startPurchaseQuery() {
		new AppPurchases(_Activity).QueryAsync();
	}
}
